package cn.learn.async.restful;

import cn.learn.async.threadpool.ThreadPoolUtil;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author huangyezhan
 * @version 1.0
 * @date 2021年08月29日21:38
 */
@RestController
public class ThreadPoolTestController {

    @RequestMapping(value = "pool/async/{id}", method = RequestMethod.GET)
    public Long testAsync(@PathVariable String id) {
        long               start    = System.currentTimeMillis();
        System.out.println(id);
        ThreadPoolExecutor executor = ThreadPoolUtil.INSTANCE.getExecutor();
        for (int i = 0; i < 20; i++) {
            executor.execute(() -> {
                try {
                    // 模拟任务耗时
                    String name = getName();
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName());
            });
        }

        return System.currentTimeMillis() - start;
    }

    @RequestMapping(value = "pool/async2", method = RequestMethod.GET)
    public String testAsync2() throws ExecutionException, InterruptedException {
        long               start    = System.currentTimeMillis();
        ThreadPoolExecutor executor = ThreadPoolUtil.INSTANCE.getExecutor();

        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return getName();
        }, executor);

        long end = System.currentTimeMillis() - start;
        System.out.println("耗时：" + end);
        return future.get();
    }

    @RequestMapping(value = "pool/async3", method = RequestMethod.GET)
    public  DeferredResult<String> testAsync3() throws ExecutionException, InterruptedException {
        long               start    = System.currentTimeMillis();
        ThreadPoolExecutor     executor = ThreadPoolUtil.INSTANCE.getExecutor();

        DeferredResult<String> result   = new DeferredResult<>();

        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return getName();
        }, executor);
        long end = System.currentTimeMillis() - start;
        System.out.println("耗时：" + end);
        result.setResult(future.get());
        return result;
    }

    private String getName(){
        return "Name";
    }




}
